package leetcode.d1;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Author: hang an
 * @Date: 2025/4/24
 * @Description:
 */
public class R2799 {
    public int countCompleteSubarrays(int[] nums) {
        int[] ans = new int[2010];
        int length = nums.length;
        for (int i : nums) {
            ans[i]++;
        }
        int len = 0;
        for (int i : ans) {
            if (i != 0) len++;
        }
        // [1,3,1,2,2] 3
        int res = 0;
        int l = 0, r = 0;
        Map<Integer, Integer> set = new HashMap<>();
        for (l = 0; l < length; l++) {
            while (r < length) {
                if (set.size() == len) {
                    res += length - r + 1;
                    if ((set.merge(nums[l], -1, Integer::sum) == 0))   {
                        set.remove(nums[l]);
                    }
                    break;
                } else {
                    set.merge(nums[r++], 1, Integer::sum);
                }
            }
        }
        return res;
    }

}
